package com.mask.im.server.netty.action.handler;

import com.mask.im.server.netty.enums.IMActionEnum;
import com.mask.im.server.netty.model.IMReceiveMessage;
import io.netty.channel.Channel;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

/**
 * @className：IMFriendChatHandler
 * @Description: 好友之间单聊
 * @Author：jian
 * @Date：2025/8/29 11:04
 */
@Log4j2
@Component
public class IMFriendChatHandler extends IMAbstractChatHandler {
    @Override
    public IMActionEnum getAction() {
        return IMActionEnum.FRIEND_CHAT;
    }

    @Override
    public void handle(IMReceiveMessage imReceiveMessage, Channel channel) {
        // 初始化错误响应DTO
//        try {
//            //校验参数
//            if (!checkRequiredParams(imReceiveMessage, channel)) {
//                return;
//            }
//            //解析并转换参数
//            Long receiverId = imReceiveMessage.getReceiverId();
//            if (receiverId == null) {
//                return;
//            }
//
//            String content = (String) imReceiveMessage.getContent();
////            content = content.replace("<p>", "<span>").replace("</p>", "</span>");
//            Integer extend = imReceiveMessage.getExtend();
//            Long userId = MaskSecurityUtil.getCurrentUserId();
//            //检查接收者在线状态
//            Channel receiverChannel = channelService.getUserIdChannel(receiverId.toString());
//            //查询好友关系（一次查询复用）
////            Friendships friendships = friendshipsService.getOne(new LambdaQueryWrapper<Friendships>()
////                    .eq(Friendships::getOneUserId, receiverId)
////                    .eq(Friendships::getTwoUserId, userId)
////                    .eq(Friendships::getIsDelete, 0));
//            Friendships friendships = new Friendships();
//            int count = null == friendships ? 2 : friendships.getIsFriends();
//            //持久化到数据库
//            Date sendTime = new Date();
//            //保存聊天记录
//            IMFriendChatMessageDO imFriendChatMessage = saveChatMessage(receiverId, content, userId, extend, WebSocketConstant.UNREAD, count, sendTime);
//
////            FriendChatMessageDTO friendChatMessageDTO = friendshipsService.getChatStorageDTO(imFriendChatMessage);
//            FriendChatMessageDTO friendChatMessageDTO = new FriendChatMessageDTO();
//            //封装消息
//            IMSendMessage websocketDTO = IMSendMessage.builder().message(friendChatMessageDTO).cmd(WebSocketConstant.SINGLE_CHAT).build();
//            //实时响应
//            if (Objects.nonNull(receiverChannel) && count == 1) {
//                receiverChannel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(websocketDTO)));
//            }
//
//            channel.writeAndFlush(new TextWebSocketFrame(JSON.toJSONString(websocketDTO)));
//            if (extend == 6){
//                //异步刷新最新消息
//                changeRecentAsync(receiverChannel, receiverId, "[语音]", userId, extend, sendTime);
//            }else {
//                changeRecentAsync(receiverChannel, receiverId, content, userId, extend, sendTime);
//            }
//
//        } catch (Exception e) {
//            log.error("单聊消息处理异常，channelId: {}", channel.id().asShortText(), e);
//        }
    }


    /**
     * 校验必要参数是否存在
     */
    private boolean checkRequiredParams(IMReceiveMessage imReceiveMessage, Channel channel) {
        if (null == imReceiveMessage.getReceiverId() ||
                null == imReceiveMessage.getContent() ||
                null == imReceiveMessage.getExtend()) {
            return false;
        }
        return true;
    }
}
